package com.qdl.rym.util.config;

import com.qdl.rym.exception.BusinessException;
import com.qdl.rym.util.DateUtil;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * @Description: UpoladFile
 * @Author: ChenXueLi
 * @Date: 2019-02-13 10:07
 * 你是一段成熟的代码，要学会自己改bug
 */
@Getter
public class UploadFile {

    /**
     * 上传的文件名称
     */
    private String fileName;

    /**
     * 保存到的位置
     */
    private String folder;

    /**
     * 上传的文件名称（原名称）
     */
    private String oldFileName;

    /**
     * 上传的文件类型
     */
    private String fileType;

    /**
     * 文件后缀
     */
    private String suffix;

    /**
     * 文件长度
     */
    private long fileSize;

    /**
     * 文件路径
     */
    private String path;

    /**
     * 文件流信息
     */
    private InputStream inputStream;

    public UploadFile(MultipartFile multipartFile) throws Exception{
        if(multipartFile == null) {
            return;
        }
        try {
            this.inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(-2001, "获取文件流错误");
        }
        this.fileName = multipartFile.getOriginalFilename();
        this.oldFileName = multipartFile.getOriginalFilename();
        this.fileSize = multipartFile.getSize();
        this.suffix = getSuffix(this.fileName);
        this.fileType = getContentType(this.suffix);
        this.folder = DateUtil.format(new Date(), DateUtil.Pattern.SYS_DATE);
        updatePath();
    }

    public UploadFile(File file) throws Exception{
        if(file == null) {
            return;
        }
        try {
            this.inputStream = new FileInputStream(file);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(-2001, "获取文件流错误");
        }
        this.fileName = file.getName();
        this.oldFileName = file.getName();
        this.fileSize = file.length();
        this.folder = DateUtil.format(new Date(), DateUtil.Pattern.SYS_DATE);
        updatePath();
    }

    /**
     * 随机文件名
     * @return 对象本身
     */
    public UploadFile randomName() {
        this.fileName = (UUID.randomUUID().toString() + this.suffix).replaceAll("-", "");
        this.suffix = getSuffix(this.fileName);
        this.fileType = getContentType(this.suffix);
        updatePath();
        return this;
    }

    /**
     * 设置文件名
     * @param fileName 文件名
     * @return 对象本身
     */
    public UploadFile setFileName(String fileName) {
        this.fileName = fileName;
        this.oldFileName = fileName;
        this.suffix = getSuffix(this.fileName);
        this.fileType = getContentType(this.suffix);
        updatePath();
        return this;
    }

    /**
     * 设置文件存放路径
     * @param folder 路径
     * @return 对象本身
     */
    public UploadFile setFolder(String folder) {
        this.folder = folder;
        updatePath();
        return this;
    }

    /**
     * 追加设置文件存放路径
     * @param subFolder 路径
     * @return 对象本身
     */
    public UploadFile addFolder(Object subFolder) {
        this.folder += "/" + subFolder.toString();
        updatePath();
        return this;
    }

    /**
     * 添加日期文件夹
     * @return
     */
    public UploadFile addDateFolder() {
        this.folder += "/" + DateUtil.format(new Date(), DateUtil.Pattern.SYS_DATE);;
        updatePath();
        return this;
    }

    /**
     * 更新文件路径
     */
    private void updatePath() {
        this.path = (this.folder + "/" + this.fileName).replaceAll("/+", "/");
    }

    /**
     * 根据文件名称获取文件后缀
     * @param fileName 文件名
     * @return 文件后缀
     */
    private String getSuffix(String fileName) {
        if (fileName == null) {
            return "";
        }
        if(!fileName.contains(".")) {
            return "";
        }
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        if(suffix.length() <= 0) {
            return "";
        }
        return suffix;
    }

    /**
     * 通过文件名判断并获取OSS服务文件上传时文件的contentType
     * @param suffix 后缀
     * @return 文件的contentType
     */
    private String getContentType(String suffix){
        String t = suffix.length() > 0 ? suffix.substring(1) : suffix;
        switch (t){
            case "bmp":                                     return "image/bmp";
            case "gif":                                     return "image/gif";
            case "jpeg": case "jpg": case "png":            return "image/jpeg";
            case "html":                                    return "text/html";
            case "txt":                                     return "text/plain";
            case "vsd":                                     return "application/vnd.visio";
            case "ppt": case "pptx":                        return "application/vnd.ms-powerpoint";
            case "doc": case "docx":                        return "application/msword";
            case "xml":                                     return "text/xml";
            case "xls": case "xlsx":                        return "application/vnd.ms-excel";
            case "sb":                                      return "application/scratch";
            case "sb2":                                     return "application/scratch2.0";
            case "sb3":                                     return "application/scratch3.0";
            case "pdf":                                     return "application/pdf";
            case "mp4":                                     return "video/mp4";
            case "mpeg": case "mpg":                        return "video/mpeg";
            default:                                        return "";
        }
    }

}
